home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DSIIC2.ARJ / L_BAR.C < prev    next >
C/C++ Source or Header  |  1991-07-15  |  5KB  |  144 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************    L_BAR.C    ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <conio.h>
  9. #include <ctype.h>
  10.  
  11.  
  12. /*****************************************************************
  13.  
  14.  Usage: int bar_menu(struct bar_struc bar_menu[], char normal,
  15.                      char inverse)
  16.  
  17.   struct bar_struc bar_menu=  data structure containing menu options.
  18.  
  19.   char normal,inverse= text attributes used for regular and
  20.                        highlighted options.
  21.  
  22.   Creates and manages a moving light bar menu at the first line of
  23.   the topmost (active) window.
  24.  
  25. *****************************************************************/
  26.  
  27. int bar_menu(struct bar_struc menu[], char normal, char inverse)
  28. {
  29.  
  30. extern struct  screen_structure scr;
  31. extern struct window_structure w[];
  32.  
  33. int x=1;       /* x screen location */
  34. int y=1;       /* y screen location */
  35. int i,j;       /* index variables */
  36. int nu_opt;    /* number of options in menu */
  37. int old_caps=scr.bold_caps;  /* original value of bold_caps */
  38.  
  39. int cur_opt;   /* current menu (highlighted option) */
  40.  
  41. char ch;       /* char and extension variables */
  42. char ext;
  43.  
  44. int found = FALSE;    /* selection made (found) flag */
  45. int return_code=0;
  46. int my_win=scr.active;
  47.  
  48. cls();
  49. cursor(NO_CURSOR); /* turn off cursor */
  50.  /* set number of options to max */
  51.  
  52. /* figure how many options there are */
  53.  
  54. for(i=0;i<MAX_BAR;i++){
  55.   if (menu[i].name[0] == '\0'){
  56.    nu_opt = i;
  57.    break;
  58.   }
  59. }
  60.  
  61. cur_opt = 0;
  62.  
  63.    for(;;){       /* loop infinite */
  64.          x=1;
  65.  
  66.           scr.bold_caps=!found;  /* turn off caps when found */
  67.  
  68.          for(i=0;i< nu_opt;i++){    /* print all options */
  69.            if(i == cur_opt) scr.current= inverse;
  70.             else scr.current= normal;
  71.             print(x,y,menu[i].name);
  72.             x=x+strlen(menu[i].name)+3; /* move option location */
  73.  
  74.          };  /* end of menu printing loop */
  75.  
  76.            if(menu[cur_opt].info[0]!='\0'){ /* is there info? */
  77.             scr.current=normal;    /* then print it on next line */
  78.             scr.bold_caps=FALSE; /* don't highlight bold caps */
  79.  
  80.             ceol(1,y+1);    /* clear to end of line
  81.                                to clear old info */
  82.             /* print new information */
  83.             print(1,y+1,menu[cur_opt].info);    
  84.            }
  85.  
  86.   if(found ){        /* selection made.
  87.                         return correct return code if function
  88.                         pointer NULL */
  89.    if(menu[cur_opt].fun==NULL) return(menu[cur_opt].select_id);
  90.       else{  /* else run option to get code */
  91.         return_code= (*menu[cur_opt].fun)() ;  
  92.         win_pop_top(my_win);
  93.         ceol(1,1);
  94.       }
  95.         found = FALSE;   /* reset flag, ready for new selection */
  96.  
  97.       if (kbhit()) getch(); /* make sure keyboard buffer is clear */
  98.  
  99.   }
  100.    else{       /* selection not made
  101.                   read keys until selection is made */
  102.  
  103.         get_key(&ch,&ext); ch=toupper(ch);  /* get a character */
  104.         if (ext == RIGHT)  cur_opt = cur_opt +1;  /* move right */
  105.         if (ext == LEFT)  cur_opt = cur_opt -1;   /* move left */
  106.         if (cur_opt >= nu_opt) cur_opt =0;        /* wrap if out of
  107.                                                      bounds */
  108.         if (cur_opt < 0) cur_opt = nu_opt-1;
  109.  
  110.         if (ch== RETURN) found = TRUE;
  111.  
  112.      /* if we have a valid character then it may be a quick key */
  113.      if(ch!='\0'){
  114.         for(i=0;i<nu_opt;i++){    /* does it match an option? */
  115.          j=0;                     /* scan each menu option name */
  116.  
  117.       /* check each letter within option */
  118.       while(menu[i].name[j]!= '\0'){
  119.           /* if match and not space */
  120.           if ( ch==menu[i].name[j++] && ch != ' '){
  121.            cur_opt = i;            /* mark found flag and break */
  122.            found = TRUE;
  123.            break;
  124.           }
  125.        }
  126.        if(found==TRUE) break;   /* break if found */
  127.      }
  128.    }
  129.            if (ch==ESCAPE){          /* EXIT IF ESCAPE KEY */
  130.             return_code = 0;         /* exit but don't close down */
  131.             break;                   /* parent menu */
  132.            }
  133.            ext=ch=' ';
  134.  
  135.            }  /* end else */
  136.           /* a non-zero return code means exit menu */
  137.           if (return_code!=0) break;
  138.  
  139.    } /* end for(;;)*/
  140.  
  141.  scr.bold_caps=old_caps; /* restore old value */
  142.  return (return_code);
  143. }
  144.